﻿<?php
//exit;
//验证签名的文件
include_once './func/common.php';
include_once './func/secureUtil.php';
include "/var/www/html/new/conf/le.je";
require("func.inc.php");//total_fee fenRun write_logs
include '/var/www/html/new/d/common/func.inc.php';//recharge_cash_back(充值金额单位元, 会员ID, 支付渠道, $soap);
$le = new go_sql();
$le->connect();

define('LOG_NAME', '/var/www/html/new/d/logs/union_pay/logs_recharge/'.date('ymd').'.log');//log文件路径
$sourceT = 2;//银联的
//$_POST = json_decode('{"accessType":"0","bizType":"000201","certId":"21267647932558653966460913033289351200","currencyCode":"156","encoding":"utf-8","merId":"802350173720081","orderId":"RECHARGE472894955b728b23ac1e","queryId":"201507281501066171218","reqReserved":"透传信息","respCode":"00","respMsg":"Success!","settleAmt":"1","settleCurrencyCode":"156","settleDate":"0728","signMethod":"01","traceNo":"617121","traceTime":"0728150106","txnAmt":"1","txnSubType":"01","txnTime":"20150728150106","txnType":"01","version":"5.0.0","signature":"i66z3CdtBs5yW3pRlOT9yl44uEBtCnvfp8g0Ii6g2p5Aaf4m\/g4xFbd0RzN1h0EQvTZlB7atNzgu8ndts35pok+FqYevewVak44v4WCc0z7IOVC5uDgpVbfXQEFKyDhgJ1yDPT9VqsTtGDYRGnYZa7bmYjR6g8zjal3y4PbD8\/OzywlV0AGTgscSAWZITWOfZtcOyZmBPd3V9iDHmQncpeRe9GZrgO\/JMgGZt8PW3WPmnsDHRzidfc\/QcI0qBBVlNP4ybnOdJaVnT8hlFsacm3+ZhdxyYtd6VSnTTcQ3puT7do3Qb9i8rXORjV6vrBzm+63h056swlCdXoxq+loNFg=="}',true);
write_logs('开始验证:
'.json_encode($_POST), LOG_NAME);

if (isset ( $_POST ['signature'] )) {
    if(!verify ( $_POST )){
        write_logs('验签失败', LOG_NAME);
        exit('chkFail');
    }
    write_logs('验签成功', LOG_NAME);
    if($_POST['respMsg']!='Success!') {
        write_logs('返回消息不成功', LOG_NAME);
        exit;
    }
    //订单号  因为长度不足 前面补上了RECHARGE三个字母
    $ordern = str_replace('RECHARGE','',$_POST['orderId']);
    $str="select status,total_fee,buyer_email from pft_alipay_rec where out_trade_no='$ordern' AND `sourceT`=$sourceT LIMIT 1";
    $GLOBALS['le']->query($str);
    $pay_log_data = $GLOBALS['le']->fetch_assoc();
    if (!$pay_log_data) {
        write_logs('支付log不存在'.$str, LOG_NAME);
        $GLOBALS['le']->close();
        exit('no data');
    }
    $status = $GLOBALS['le']->f('status');
    if($status!=0){
        write_logs('非正常状态{'.$status.'}', LOG_NAME);
        $GLOBALS['le']->close();
        exit('fail');
    }else{
        $pay_total_fee = (int)$_POST['txnAmt'];                //回传的信息里单位是分
        $total_fee = (string)($GLOBALS['le']->f("total_fee") * 100);//数据库中的单位是元
        if ($pay_total_fee != $total_fee) {
            write_logs('支付金额不等。返回金额:' . $pay_total_fee . ',实际金额:'. $total_fee, LOG_NAME);
            $GLOBALS['le']->close();
            exit('fail');
        }
        $sid =$GLOBALS['le']->f('buyer_email');
        $s = new SoapClient(null,array(
            "location" => "http://localhost/open/openService/pft_insideMX.php",
            "uri" => "www.16u.com?ac_16u=16ucom|pw_16u=c33367701511b4f6020ec61ded352059|auth_16u=true"));
        //管理员帐号加钱
        //$s->PFT_Member_Fund_Modify(1, 0, $pay_total_fee, 0, 0,null,3, 0,'','银联充值');
        //客户帐号加钱
        if(($res=$s->PFT_Member_Fund_Modify($sid,$sid,$pay_total_fee,0,0,null,3,0,'','银联充值'))!=100){
            write_logs('客户帐号加钱失败,原因:'.$res,LOG_NAME);
            $GLOBALS['le']->close();
            exit;
        }
        recharge_cash_back($total_fee/100, $sid, 5, $s);
        $trade_no = $_POST['queryId'];//银联的交易流水号
        //$buyer_email = $_POST['accNo'];// 没有这个字段
        $str=<<<SQL
UPDATE pft_alipay_rec set status=1,dtime={$_POST['txnTime']},`trade_no`='$trade_no'/
WHERE out_trade_no='$ordern' AND `sourceT`=$sourceT limit 1
SQL;
        $GLOBALS['le']->query($str);
        $GLOBALS['le']->close();
        $p_type = 5;//银联支付
        $GLOBALS['le']->close();
        write_logs('后台处理完毕', LOG_NAME);
    }
}else {
    write_logs('签名为空', LOG_NAME);
    exit('chkLost');
}

?>
